package inf.specimensManagement;

import inf.booksManagement.Book;
import inf.exceptions.BookedSpecimenException;
import inf.exceptions.NotLentMediaException;
import inf.loansManagement.Loan;
import inf.loansManagement.Booking;
import inf.mediasManagement.Media;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

/*
 * Class representing a specimen of a book
 *  * @version 0.0.1
 */

/**
 * Entity implementation class for Entity: Specimen
 *
 */
@Entity
@Table(name="specimens")
public class Specimen{

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="id",unique = true,nullable=false)
	private long id;

	/**
	 * Identifier of the media + specimen
	 */
	private String number;
	
	@ManyToOne
	@JoinColumn(name = "media_id")
	private Media media;

	@OneToOne
	@JoinColumn(name = "loan_id")
	private Loan loan;
	
	@OneToOne
	@JoinColumn(name = "booking_id")
	private Booking booking;

	public Specimen(){}

	public Specimen(Media media){
		this.media = media;
		this.setNumber(media.getIsbnOrIsan() + ":" + media.getNbCreatedSpecimen());
	}	
	
	public boolean isLent() {
		return (loan != null);
	}

	public boolean isBooked() {
		return (booking != null);
	}

	public void endOfBooking() throws NotLentMediaException {
		if(this.booking == null)
			throw new NotLentMediaException();

		this.setBooking(null);
	}

	public List<String> history(){
		List<Loan> history = inf.loansManagement.LoanDAO.getBySpecimenId(this.getId());
		List<String> retour = new ArrayList<String>();
		for(Loan loan : history){
			retour.add(loan.toString());
		}
		return retour;
	}
	
	/**  
	 * Accesseurs
	 */
	
	@Override
	public String toString(){
		return "the specimen " + id + " of " + media.getTitle();
	}
	/**
	 * Getter of the property <tt>id</tt>
	 * @return  Returns the id.
	 * @uml.property  name="id"
	 */
	public long getId() {
		return id;
	}
	/**
	 * Setter of the property <tt>id</tt>
	 * @param specId  The id to set.
	 * @uml.property  name="id"
	 */
	public void setId(long specId) {
		this.id = specId;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	/**
	 * Getter of the property <tt>media</tt>
	 * @return  Returns the media.
	 * @uml.property  name="media"
	 */
	public Media getMedia() {
		return media;
	}
	/**
	 * Setter of the property <tt>media</tt>
	 * @param media  The media to set.
	 * @uml.property  name="media"
	 */
	public void setMedia(Media media) {
		this.media = media;
	}
	public Loan getLoan() {
		return loan;
	}
	public void setLoan(Loan loan) {
		this.loan = loan;
	}
	public Booking getBooking() {
		return booking;
	}
	public void setBooking(Booking booking) {
		this.booking = booking;
	}
}
